<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form onsubmit="return false" >

        <textarea name="message" style="width:300px; height: 300px"></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseData" style="width: 300px; height: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseData').value = ''">

    </form>
</body>

<script >
    var socket = null;

    if (!window.WebSocket) {
        alert("浏览器不支持websocket")
    } else {
        socket = new WebSocket("ws://localhost:8999/hello");
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseData");
            rt.value = "服务器已连接...";
        }

        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseData");
            rt.value = rt.value + "\n" + ev.data;
        }

        socket.onclose  = function (ev) {
            var rt = document.getElementById("responseData");
            rt.value = rt.value + "\n" + "服务器已关闭...";
        }
    }


    function send(message) {
        if (!window.socket) {
            alert("socket 未打开")
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("连接未打开")
        }
    }


</script>
</html>